package com.aofeng.hybridwork.android.task;

import android.app.ProgressDialog;
import android.os.AsyncTask;

import com.aofeng.hybrid.activity.HybridActivity;
import com.aofeng.hybrid.orm.SqlHelper;
import com.aofeng.hybrid.service.RestService;
import com.aofeng.hybrid.util.CommUtil;
import com.aofeng.hybridwork.application.HybridApplication;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;

/**
 * 更新安检单审核情况异步任务
 * Created by LGY on 2016-08-25.
 */
public class RefreshRepairApprovalTask extends AsyncTask<String, String, Boolean> {
    private static final Logger log = LoggerFactory.getLogger(RefreshRepairApprovalTask.class);

    HybridActivity hybridActivity;

    public RefreshRepairApprovalTask(HybridActivity hybridActivity) {
        this.hybridActivity = hybridActivity;
    }

    @Override
    /**
     * params:
     * 计划id
     */
    protected Boolean doInBackground(String... params) {
        try {
            publishProgress(new String[]{0 + "", "开始更新......"});

            RestService rs = new RestService(hybridActivity);
            JSONObject joRequest = new JSONObject();
            joRequest.put("action", "post");
            joRequest.put("url", HybridApplication.SQL_SERVICE_URL + URLEncoder.encode("获取维修审核状态", "UTF-8"));
            joRequest.put("data", "{\"data\":{\"id\":\"" + params[0] + "\"}}");
            JSONArray array = (JSONArray) rs.execute(joRequest);

            publishProgress(new String[]{30 + "", "下载更新数据......"});

            int n = array.length();
            for (int i = 0; i < n; i++) {
                JSONObject jo = array.getJSONObject(i);
                SqlHelper.run(hybridActivity,
                        "update t_check_paper set "
                                + "f_repair_approved = '" + jo.getString("f_repair_approved") + "', "
                                + "f_repair_approved_by='" + jo.getString("f_repair_approved_by") + "', "
                                + "f_repair_approved_time='" + jo.getString("f_repair_approved_time") + "', "
                                + "f_repair_approved_note='" + (jo.isNull("f_repair_approved_note") ? "" : jo.getString("f_repair_approved_note")) + "' "
                                + "where id = '" + jo.getString("id") + "'"
                );
                publishProgress(new String[]{(30 + (i * 70) / n) + "", "更新审核状态" + i + "/" + n});
            }

            publishProgress(new String[]{100 + "", "更新完成。"});
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        hybridActivity.createProgressDialog(ProgressDialog.STYLE_SPINNER);
    }

    @Override
    protected void onPostExecute(Boolean flag) {
        super.onPostExecute(flag);
        hybridActivity.closeProgress();
        hybridActivity.mWebView.loadUrl("javascript:HostApp.__callback__(" + flag + ");");
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        int progress = Integer.parseInt(values[0]);
        hybridActivity.setProgress(progress, values[1]);
    }
}
